<?php

class Sistema_Form_Revenda extends Zend_Form {

    public function init() {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;

        $this->setName('form_pdr');

        $id = $front->getRequest()->getParam('id', 0);
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $query = 'SELECT SQL_CACHE * FROM REVENDA WHERE REV_ID = ' . $id;
        $data = $db->fetchRow($query);
        
        $el = new Zend_Form_Element_Hidden('REV_ID', array('disableLoadDefaultDecorators' => true));
        $el->addDecorators(array(array('ViewHelper'), array('Errors')))->addFilter('Int');
        $this->addElement($el);


        $el = new Zend_Form_Element_Text('REV_NOM');
        $el->setLabel('Nome')->setRequired(true);
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REV_NOM');
        $el->setLabel('Nome')->setRequired(true);
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REV_TEL');
        $el->setLabel('Telefone');
        $el->setAttrib('class', 'telefone');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REV_EML');
        $el->setLabel('E-mail');
        $el->setAttrib('class', 'email');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REV_SIT');
        $el->setLabel('Site');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REV_MAP');
        $el->setLabel('Google Maps');
        $this->addElement($el);
        
        $arr = array(); 
        $query = 'SELECT SQL_CACHE EST_ID,EST_UF FROM ESTADOS ORDER BY EST_UF ASC';
        $result = $db->fetchAll($query);
        foreach($result as $row) $arr[$row['EST_ID']] = $row['EST_UF']; 
        $el = new Zend_Form_Element_Select('EST_ID',array('onchange' => ''));
        $el->setLabel('UF')->setRequired(true);
        $el->addMultiOptions($arr);
        $this->addElement($el);
        
        $arr = array(''=>'Nenhum'); 
        if($data['EST_ID']){
            $query = 'SELECT SQL_CACHE CID_ID,CID_NOM FROM CIDADES WHERE EST_ID = '.$data['EST_ID'].' ORDER BY CID_NOM ASC';
            $result = $db->fetchAll($query);
            foreach($result as $row) $arr[$row['CID_ID']] = $row['CID_NOM']; 
        }
        $el = new Zend_Form_Element_Select('CID_ID',array('onchange' => ''));
        $el->setLabel('Cidade')->setRequired(true);
        $el->addMultiOptions($arr);
        $el->setRegisterInArrayValidator(false);
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REV_BAI');
        $el->setLabel('Bairro');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REV_END');
        $el->setLabel('Endereço');
        $this->addElement($el);
        
        $img = glob('public/upload/'.$controller.'/'.$id.'.*');
        if(is_array($img))
        foreach($img as $src) {
            $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>'<img height="150" style="max-width:600px" src="'.$front->getBaseUrl().'/'.$src.'" />' ));
            $this->addElement($el);
        }
        $el = new Zend_Form_Element_File('REV_FILE');
        $el->setLabel('Imagem - Resolução: 350 x 150'); 
        $el->addValidator('Extension', false, array('jpg', 'jpeg', 'png', 'gif'));
        $this->addElement($el);
        
        
        $el = new Zend_Form_Element_Checkbox('REV_STA',array('checked'=>'checked'));
        $el->setLabel('Ativo');
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('Salvar', array('disableLoadDefaultDecorators' => true));
        $btn->setLabel('Salvar');
        $btn->addDecorators(array(array('ViewHelper'), array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);

        $btn = new Zend_Form_Element_Button('Cancelar', array('disableLoadDefaultDecorators' => true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="' . $url . '" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
    }

}
